package com.my.blogs.personblog.controller;

import com.my.blogs.personblog.Service.UserService;
import com.my.blogs.personblog.enity.Code;
import com.my.blogs.personblog.enity.Result;
import com.my.blogs.personblog.enity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseEntity<Result> login(@RequestBody User user1) {
        Result result;
        User user = userService.login(user1.getUsername(), user1.getPassword());
        if (user == null) {
            result = new Result(Code.FAIL, "用户名或密码错误", null);
        } else {
            result = new Result(Code.SUCCESS, "登录成功", user);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/register")
    public ResponseEntity<Result> register(@RequestBody User user1) {
        Result result;
        User user = userService.register(user1.getUsername(), user1.getPassword(), user1.getEmail(), user1.getPhone(), user1.getIdCard());
        if (user == null) {
            result = new Result(Code.FAIL, "用户名已存在", null);
        } else {
            result = new Result(Code.SUCCESS, "注册成功", user);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/select/all")
    public ResponseEntity<Result> selectAllUsers() {
        Result result;
        List<User> users = userService.selectAllUsers();
        if (users == null) {
            result = new Result(Code.FAIL, "用户列表为空", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", users);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/select/{id}")
    public ResponseEntity<Result> selectUserById(@PathVariable Integer id) {
        Result result;
        User user = userService.selectUserById(id);
        if (user == null) {
            result = new Result(Code.FAIL, "用户不存在", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", user);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/select/{username}")
    public ResponseEntity<Result> selectUserByUsername(@PathVariable String username) {
        Result result;
        User user = userService.selectUserByUsername(username);
        if (user == null) {
            result = new Result(Code.FAIL, "用户不存在", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", user);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/select/{email}")
    public ResponseEntity<Result> selectUserByEmail(@PathVariable String email) {
        Result result;
        User user = userService.selectUserByEmail(email);
        if (user == null) {
            result = new Result(Code.FAIL, "用户不存在", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", user);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/select/{phone}")
    public ResponseEntity<Result> selectUserByPhone(@PathVariable String phone) {
        Result result;
        User user = userService.selectUserByPhone(phone);
        if (user == null) {
            result = new Result(Code.FAIL, "用户不存在", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", user);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/insert")
    public ResponseEntity<Result> insertUser(User user) {
        Result result;
        Boolean status = userService.insertUser(user);
        if (status) {
            result = new Result(Code.SUCCESS, "插入成功", user);
        } else {
            result = new Result(Code.FAIL, "插入失败", user);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/update")
    public ResponseEntity<Result> updateUser(User user) {
        Result result;
        Boolean status = userService.updateUser(user);
        if (status) {
            result = new Result(Code.SUCCESS, "更新成功", user);
        } else {
            result = new Result(Code.FAIL, "更新失败", user);
        }
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Result> deleteUser(@PathVariable Integer id) {
        Result result;
        Boolean status = userService.deleteUser(id);
        if (status) {
            result = new Result(Code.SUCCESS, "删除成功");
        } else {
            result = new Result(Code.FAIL, "删除失败");
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/sort/{id}")
    public ResponseEntity<Result> sortById(@PathVariable Integer id) {
        Result result;
        List<User> users = userService.sortById(id);
        if (users == null) {
            result = new Result(Code.FAIL, "用户列表为空", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", users);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/page/{page}/{pageSize}")
    public ResponseEntity<Result> selectPage(@PathVariable String page, @PathVariable String pageSize) {
        Result result;
        List<User> users = userService.selectPage(Integer.parseInt(page), Integer.parseInt(pageSize));
        if (users == null) {
            result = new Result(Code.FAIL, "用户列表为空", null);
        } else {
            result = new Result(Code.SUCCESS, "查询成功", users);
        }
        return ResponseEntity.ok(result);
    }
}
